<?php
/**产品回收估价
 *
 *   */
namespace Api\Controller;
use Think\Controller;
use Api\Model\NewsModel;
class ProductRecycleController extends PublicController {
    private $_pname = '';
    /**
     * 二手手机获取手机型号最高估价接口
     * @param string  pro_model  手机型号
     *    */
    public function getValuation(){
        $this->_pname = $_REQUEST['pro_model'];
//        $this->_checkParameter();
        $where = array(
            'pname' => $this->_pname
        );
        $price = M('sp_product')->where($where)->find();
        if ($price){
            echo json_encode(array('status'=>200,'info'=>$price));
            exit();
        }
        echo json_encode(array('status'=>5030,'err'=>'暂无报价'));
        exit();

    }
    /**获取估价商品属性
     * @param string  pro_model  手机型号
     *
     *  */
    public function getProduct(){
        $id = $_REQUEST['id'];
        $where = array(
            'id' => $id
        );
        $p_attr_data = M('sp_product')->where($where)->find();
        if ($p_attr_data){
            $p_attr_data['content'] = json_decode($p_attr_data['content']);
            exit(json_encode(array('status'=>200,'info'=>$p_attr_data)));
        }else{
            exit(json_encode(array('status'=>5031,'err'=>'此商品暂无估价')));
        }
    }
    /**开始估价
     * @param string  pro_model  手机型号
     * @param string  sku_str      属性:属性值;属性:属性值;
     *  */
    public function getEndvaluation(){
        $id = $_REQUEST['id'];
        $reduce_price = $_REQUEST['reduce_price'];
        $where = array(
            'id' => $id
        );
/*        $content = M('sp_product')->where('pname='.$this->_pname)->getFiled('content');
        $content = json_decode($content);
        $sku_str = $_REQUEST['sku_str'];
        $p_attribute = explode(";",$sku_str);
        $p_attributeData = array();
        foreach ($p_attribute as $pk => $pv){
            $p_attributeData[] = explode(":",$pv);
        }
        $reduce_price = 0;
        foreach ($content as $ck => $cv){
            foreach ($p_attributeData as $pk2 => $pv2){
                if ($cv['title'] == $pv2[0]){
                    foreach ($cv['choix'] as $chk => $chv){
                        if ($chv['name'] == $pv2[1]){
                            $reduce_price += $chv['price'];
                        }
                    }
                }
            }
        }*/
        $price = M('sp_product')->where($where)->find();
        $endprice = round(($price-$reduce_price) * 100) / 100;
        if ($endprice){
            exit(json_encode(array('status'=>200,'endprice'=>$endprice,'pro_model'=>$id)));
        }else{
            exit(json_encode(array('status'=>5032,'err'=>'此商品暂无估价')));
        }

    }
    /**
     * 获取回收产品品牌
     *   */
    public function getBrandlist(){
        $brandlist = M('brand')->select();
        array_unshift($brandlist, array('id' => '0', 'name' => '全部'));
        exit(json_encode(array('status'=>200,'info'=>$brandlist)));
    }
    /**
     * 获取回收产品
     * @param int brandid  品牌id*/
    public function getRecycleProduct(){
        $brandid = intval($_REQUEST['brandid']);
        if ($brandid == 0) {
            $product = M('sp_product')->field('pname, id')->select();
        } else {
            $product = M('sp_product')->where('brand='.$brandid)->field('pname, id')->select();
        }
        if ($product){
            exit(json_encode(array('status'=>200,'info'=>$product)));
        }else{
            exit(json_encode(array('status'=>5030,'err'=>'此品牌暂无商品')));
        }
    }

    private function _checkParameter(){
        if (empty($this->_pname)) {
            echo json_encode(array('status'=>201,'err'=>'参数为空'));
            exit();
        }
    }

}












